/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { base32 } from './hi-base32.js'
import { imageData } from './imageData.js';

let encodeText = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. Suspendisse lectus tortor, dignissim sit amet, adipiscing nec, ultricies sed, dolor. Cras elementum ultrices diam. Maecenas ligula massa, varius a, semper congue, euismod non, mi. Proin porttitor, orci nec nonummy molestie, enim est eleifend mi, non fermentum diam nisl sit amet erat. Duis semper. Duis arcu massa, scelerisque vitae, consequat in, pretium a, enim. Pellentesque congue. Ut in risus volutpat libero pharetra tempor. Cras vestibulum bibendum augue. Praesent egestas leo in pede. Praesent blandit odio eu enim.';
let enCodeResult = 'JRXXEZLNEBUXA43VNUQGI33MN5ZCA43JOQQGC3LFOQWCAY3PNZZWKY3UMV2HK4RAMFSGS4DJONRWS3THEBSWY2LUFYQFGZLEEBXG63RAOJUXG5LTFYQFG5LTOBSW4ZDJONZWKIDMMVRXI5LTEB2G64TUN5ZCYIDENFTW42LTONUW2IDTNF2CAYLNMV2CYIDBMRUXA2LTMNUW4ZZANZSWGLBAOVWHI4TJMNUWK4ZAONSWILBAMRXWY33SFYQEG4TBOMQGK3DFNVSW45DVNUQHK3DUOJUWGZLTEBSGSYLNFYQE2YLFMNSW4YLTEBWGSZ3VNRQSA3LBONZWCLBAOZQXE2LVOMQGCLBAONSW24DFOIQGG33OM52WKLBAMV2WS43NN5SCA3TPNYWCA3LJFYQFA4TPNFXCA4DPOJ2HI2LUN5ZCYIDPOJRWSIDOMVRSA3TPNZ2W23LZEBWW63DFON2GSZJMEBSW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XEYLUFYQEI5LJOMQHGZLNOBSXELRAIR2WS4ZAMFZGG5JANVQXG43BFQQHGY3FNRSXE2LTOF2WKIDWNF2GCZJMEBRW63TTMVYXKYLUEBUW4LBAOBZGK5DJOVWSAYJMEBSW42LNFYQFAZLMNRSW45DFONYXKZJAMNXW4Z3VMUXCAVLUEBUW4IDSNFZXK4ZAOZXWY5LUOBQXIIDMNFRGK4TPEBYGQYLSMV2HEYJAORSW24DPOIXCAQ3SMFZSA5TFON2GSYTVNR2W2IDCNFRGK3TEOVWSAYLVM52WKLRAKBZGCZLTMVXHIIDFM5SXG5DBOMQGYZLPEBUW4IDQMVSGKLRAKBZGCZLTMVXHIIDCNRQW4ZDJOQQG6ZDJN4QGK5JAMVXGS3JO';
let decodeResult = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. Suspendisse lectus tortor, dignissim sit amet, adipiscing nec, ultricies sed, dolor. Cras elementum ultrices diam. Maecenas ligula massa, varius a, semper congue, euismod non, mi. Proin porttitor, orci nec nonummy molestie, enim est eleifend mi, non fermentum diam nisl sit amet erat. Duis semper. Duis arcu massa, scelerisque vitae, consequat in, pretium a, enim. Pellentesque congue. Ut in risus volutpat libero pharetra tempor. Cras vestibulum bibendum augue. Praesent egestas leo in pede. Praesent blandit odio eu enim.';

function encode_Test() {
  let enCodeStr = ''
  let startTime = Date.now();
  for (let i = 0; i < 1000; i++) {
    enCodeStr = base32.encode(encodeText);
  }
  let endTime = Date.now();
  if (enCodeStr != enCodeResult) {
    throw 'base32.encode error'
  }
  print(`hi-base32_encode: ${endTime - startTime} ms`)

}

function decode_Test() {
  let decodeStr = ''
  let startTime = Date.now();
  for (let i = 0; i < 1000; i++) {
    decodeStr = base32.decode(decodeText);
  }
  let endTime = Date.now();
  if (decodeStr != decodeResult) {
    throw 'base32.decode error'
  }
  print(`hi-base32_decode: ${endTime - startTime} ms`)
}

function encode_Test_1() {
  let enCodeStr = ''
  let decodeData = ''
  let startTime = Date.now();
  for (let i = 0; i < 100; i++) {
    enCodeStr = base32.encode(imageData);
  }
  decodeData = base32.decode.asBytes(enCodeStr);
  let endTime = Date.now();
  if (decodeData.length != imageData.length) {
    throw 'base32.encode error'
  }
  print(`hi-base32_encode(imageData): ${endTime - startTime} ms`)

}


function decode_Test_1() {
  let enCodeStr = base32.encode(imageData);
  let decodeData = ''
  let startTime = Date.now();
  for (let i = 0; i < 100; i++) {
    decodeData = base32.decode.asBytes(enCodeStr);
  }
  let endTime = Date.now();
  if (decodeData.length != imageData.length) {
    throw 'base32.decode error'
  }
  print(`hi-base32_decode(imageData): ${endTime - startTime} ms`)
}


let content = 'ABCDEFGHIJKabcdefj123456+/AA==!@';
let encodes = base32.encode(content);
let result = ""
function fun1() {
  let arr = new Array(10)
  let len = arr.length
  let start = Date.now()
  for (let i = 0; i < 2000; i++) {
    arr[i % len] = base32.encode(content);
  }
  let end = Date.now()
  result += ("hi-base32_encode[32byte]: " + (end - start) + " ms" + "\n")
}

function fun2() {
  let arr = new Array(10)
  let len = arr.length
  let start = Date.now()
  for (let i = 0; i < 2000; i++) {
    arr[i % len] = base32.decode(encodes);
  }
  let end = Date.now()
  result += ("hi-base32_decode[32byte]: " + (end - start) + " ms" + "\n")
}

let dic = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
let content1k = ""
for (let i = 0; i < 1024; i++) {
  content1k += dic[i / 10]
}
let content5k = ""
let num = 5120
for (let i = 0; i < num; i++) {
  content5k += dic[i / 10]
}

let content100k = ""
let num100 = 5120*2;
for (let i = 0; i < num100; i++) {
  content100k += dic[i / 10]
}

let encode1 = base32.encode(content1k);
let encode2 = base32.encode(content5k);
let encode3 = base32.encode(content100k);
function fun3() {
  let arr = new Array(10)
  let len = arr.length
  let start = Date.now()
  for (let i = 0; i < 200; i++) {
    arr[i % len] = base32.encode(content1k);
  }
  let end = Date.now()
  result += ("hi-base32_encode[1k]: " + (end - start) + " ms" + "\n")
}

function fun4() {
  let arr = new Array(10)
  let len = arr.length
  let start = Date.now()
  for (let i = 0; i < 200; i++) {
    arr[i % len] = base32.decode(encode1);
  }
  let end = Date.now()
  result += ("hi-base32_decode[1k]: " + (end - start) + " ms" + "\n")
}

function fun5() {
  let arr = new Array(10)
  let len = arr.length
  let start = Date.now()
  for (let i = 0; i < 200; i++) {
    arr[i % len] = base32.encode(content5k);
  }
  let end = Date.now()
  result += ("hi-base32_encode[5k]: " + (end - start) + " ms" + "\n")
}    

function fun6() {
  let arr = new Array(10)
  let len = arr.length
  let start = Date.now()
  for (let i = 0; i < 200; i++) {
    arr[i % len] = base32.encode(encode2);
  }
  let end = Date.now()
  result += ("hi-base32_decode[5k]: " + (end - start) + " ms" + "\n")
}

function fun7() {
  let arr = new Array(10)
  let len = arr.length
  let start = Date.now()
  for (let i = 0; i < 1; i++) {
    arr[i % len] = base32.encode(content100k);
  }
  let end = Date.now()
  result += ("hi-base32_encode[100k]: " + (end - start) + " ms" + "\n")
}

function fun8() {
  let arr = new Array(10)
  let len = arr.length
  let start = Date.now()
  for (let i = 0; i < 1; i++) {
    arr[i % len] = base32.encode(encode3);
  }
  let end = Date.now()
  result += ("hi-base32_decode[100k]: " + (end - start) + " ms" + "\n")
}


let loopCountForPreheat = 1;
for (let i = 0; i < loopCountForPreheat; i++) {
  encode_Test();
  decode_Test();
  encode_Test_1();
  decode_Test_1();

  fun1(), fun2(), fun3(), fun4(), fun5(), fun6(), fun7(), fun8();

  print(result)
}

ArkTools.waitAllJitCompileFinish();

encode_Test();
decode_Test();
encode_Test_1();
decode_Test_1();

fun1(), fun2(), fun3(), fun4(), fun5(), fun6(), fun7(), fun8();

print(result)


